package cn.biplam.front.template.control.tree;

import cn.biplam.common.wrapper.ErrorMessage;
import cn.biplam.front.html.HtmlDiv;
import cn.biplam.front.html.HtmlImage;
import cn.biplam.front.html.base.HtmlWindow;
import cn.biplam.front.html.base.Const.Visibility_Mode;

/**
 * 树自定义图标。自定义图片根据树节点的属性来设置，
 *      及同一属性的节点对应同一类型的图标	。
 *      非叶子节点的图片必须有两张 picName(收起后显示的图片)和opicName(展开后显示的图片)
 *      叶子节点的图片和主题叶子节点的图片是一张 picName(收起和展开显示同一图片)
 * @author 	WangSong 2009-4-23 下午04:16:34
 * @version 1.0
 */
public class Icon {

	/**节点图标 */
	private HtmlWindow icon;

	private String sHomeUrl;
	/**图片名称 */
	private String imageName;
	/**图片路径 */
	private String picPath;
	/**当前节点级别 */
	private int level;
	/**当前节点向下取的级别 */
	private int levelDown;

	/**
	 * 构造方法
	 * @param sHomeUrl    系统路径
	 * @param imageName   图片名称
	 * @param level       当前节点级别
	 * @param levelDown   当前节点向下取的级别
	 * @throws ErrorMessage
	 */
	public Icon(String sHomeUrl,String imageName,int level,int levelDown) throws ErrorMessage{
		this.sHomeUrl = sHomeUrl;
		this.icon = new HtmlDiv(null,"ti");
		this.imageName = imageName;
		this.picPath = this.sHomeUrl + "/images/";
		this.level = level;
		if(level<levelDown){
			this.levelDown = levelDown;
		}else{
			this.levelDown = level;
		}
	}

	/**
	 * 取节点图标
	 * @param ID    节点图标id (tree_Id+","+node_id)
	 * @param nodeType  节点类型
	 * @return HtmlWindow
	 * @throws ErrorMessage 
	 */
	public HtmlWindow get(String ID,char isLeaf,boolean isRoot) throws ErrorMessage{
		ID = ID+"icon,";
		this.getPic(ID, isLeaf,isRoot);
		return this.icon;
	}


	/**
	 * 获取图片显示页面
	 * @param ID
	 * @param isLeaf
	 * @param isRoot
	 * @throws ErrorMessage
	 */
	private void getPic(String ID,char isLeaf,boolean isRoot) throws ErrorMessage{
		if(isLeaf == '1'){/**不是叶子节点*/
			HtmlWindow open = this._getIcon(ID+"s",this.picPath+"o"+this.imageName);
			HtmlWindow uopen = this._getIcon(ID+"u",this.picPath+this.imageName);
			if(this.level<this.levelDown){
				uopen.setVisibility(Visibility_Mode.Hidden);
			}else{
				open.setVisibility(Visibility_Mode.Hidden);
			}
			this.icon.put(open);
			this.icon.put(uopen);
		}else{
			this.icon.put(this._getIcon(ID+"u",this.picPath+this.imageName));
		}
	}

	/**
	 * 创建一个用于图片切换的内置Div
	 */
	private HtmlWindow _getIcon(String ID,String imageUrl) throws ErrorMessage{
		HtmlWindow div = new HtmlDiv(ID,"tinorm");
		HtmlWindow mImage=new HtmlImage(imageUrl);
		div.put(mImage);
		return div;
	}

}
